package cn.com.hhrcw.personal.wxlogin.web;

import cn.com.hhrcw.entity.CmsContent;
import cn.com.hhrcw.entity.SysUser;
import cn.com.hhrcw.personal.wxlogin.util.Constant;
import cn.com.hhrcw.personal.wxlogin.util.WxBindTokenUtil;
import cn.com.hhrcw.service.ICmsContentService;
import cn.com.hhrcw.service.IMemberThirdService;
import cn.com.hhrcw.utils.SubjectUtils;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 微信登录-PC端
 *
 * @author lixingxing
 * @since 2020-11-06
 */
@RestController
@Slf4j
@RequestMapping("/personal/wxBind/pc")
@Validated
public class WxBindPCController extends JeecgController<CmsContent, ICmsContentService> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IMemberThirdService memberThirdService;

    @Value("${spring.profiles.active}")
    private String profiles;

    /**
     * 生成绑定token(PC 端调用)
     *
     * @return
     */
    @PostMapping(value = "/setToken")
    public Result<?> setToken() {

        String token = WxBindTokenUtil.createToken();
        //默认1分钟失效
        final long time = StrUtil.equals(profiles, "dev") ? (Constant.TIME_MIN * 24) : Constant.TIME_MIN;

        SysUser sysUser = (SysUser) SubjectUtils.getSubject();
        String userId = StrUtil.equals(profiles, "dev") ? "63abc1c1251266d73965baebc70d94ca" : sysUser.getId();

        redisTemplate.opsForValue().set(token, Constant.WAIT, time, TimeUnit.MINUTES);
        redisTemplate.opsForValue().set(WxBindTokenUtil.getUserIdToken(token), userId, time, TimeUnit.MINUTES);

        log.info("profiles {} token:{},userId:{} ,失效时间：{}分钟", profiles, token, userId, time);
        return Result.ok(token);
    }

    /**
     * 检查情况
     *
     * @return
     */
    @GetMapping(value = "/check")
    public Result<?> check(String token) {//

        log.info(token);

        final String loginState = (String) redisTemplate.opsForValue().get(token);
        log.info("loginState： {} ,userId:{},wxObj:{} ", loginState);
        if (StringUtils.isBlank(loginState)) {
            return Result.error(-1, "token不存在");
        }
        //OK登录
        else if (loginState.equalsIgnoreCase(cn.com.hhrcw.personal.wxlogin.util.Constant.OK)) {
            return Result.ok(loginState);
        } else {//
            return Result.ok(loginState);
        }

    }

}
